package com.yunwaikeji.gy_tool_library.utils;

import android.graphics.Color;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SnapHelper;

import com.yunwaikeji.gy_tool_library.info.GYRecyclerInfo;
import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GYRecyclerViewUtils implements GYRecyclerInfo {
    private static GYRecyclerViewUtils gyRecyclerViewUtils;

    public static GYRecyclerViewUtils getInstance() {
//        if (gyRecyclerViewUtils == null) {
//            gyRecyclerViewUtils = new GYRecyclerViewUtils();
//        }
        return new GYRecyclerViewUtils();
    }

    private RecyclerView.ItemDecoration itemDecoration;

    public GYRecyclerViewUtils setItemDecoration(RecyclerView.ItemDecoration itemDecoration) {
        this.itemDecoration = itemDecoration;
        return this;
    }

    /**
     * =============================================================================================
     * 横纵
     *
     * @param recyclerViews
     * @return
     */
    public LinearLayout linearLayout(RecyclerView... recyclerViews) {
        return linearLayout(Arrays.asList(recyclerViews));
    }

    public LinearLayout linearLayout(List<RecyclerView> recyclerViewList) {
        return new LinearLayout(recyclerViewList);
    }

    public class LinearLayout {
        private List<RecyclerView> recyclerViewList;

        public LinearLayout(List<RecyclerView> recyclerViewList) {
            this.recyclerViewList = recyclerViewList;
        }

        private int dividerPx = 0;
        private int dividerDp = WRAP_CONTENT;
        private int dividerColor = Color.TRANSPARENT;
        private List<Integer> dividerPositionList;
        private boolean scrollMany = false;
        private SnapHelper snapHelper;

        public LinearLayout setDividerPx(int dividerPx) {
            this.dividerPx = dividerPx;
            dividerDp = WRAP_CONTENT;
            return this;
        }

        public LinearLayout setDividerDp(int dividerDp) {
            this.dividerDp = dividerDp;
            return this;
        }

        public LinearLayout setDividerColor(int dividerColor) {
            this.dividerColor = dividerColor;
            return this;
        }

        public LinearLayout setDividerPositions(List<Integer> dividerPositionList) {
            this.dividerPositionList = dividerPositionList;
            return this;
        }

        public LinearLayout setDividerPositions(Integer... dividerPositions) {
            return setDividerPositions(Arrays.asList(dividerPositions));
        }

        public LinearLayout setDividerPosition(int dividerStart, int dividerEnd) {
            if (dividerStart <= dividerEnd) {
                dividerPositionList = new ArrayList<>();
                for (int i = dividerStart; i <= dividerEnd; i++) {
                    dividerPositionList.add(i);
                }
            }
            return this;
        }

        public LinearLayout setDividerPositionAll() {
            dividerPositionList = null;
            return this;
        }

        public LinearLayout setScrollMany(boolean scrollMany) {
            this.scrollMany = scrollMany;
            return this;
        }

        public LinearLayout setSnapHelper(SnapHelper snapHelper) {
            this.snapHelper = snapHelper;
            return this;
        }

        public GYRecyclerViewUtils buildVertical() {
            return build(DisplayStyle.VERTICAL);
        }

        public GYRecyclerViewUtils buildHorizontal() {
            return build(DisplayStyle.HORIZONTAL);
        }

        public GYRecyclerViewUtils buildPagerVertical() {
            return build(DisplayStyle.PAGER_VERTICAL);
        }

        public GYRecyclerViewUtils buildPagerHorizontal() {
            return build(DisplayStyle.PAGER_HORIZONTAL);
        }

        public GYRecyclerViewUtils build(int orientation) {
            for (RecyclerView recyclerView : recyclerViewList) {
                if (dividerDp != WRAP_CONTENT) {
                    dividerPx = GYDisplayUtils.dp2px(recyclerView.getContext(), dividerDp);
                }
                switch (orientation) {
                    case DisplayStyle.VERTICAL:
                        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext(), LinearLayoutManager.VERTICAL, false));
                        break;
                    case DisplayStyle.PAGER_VERTICAL:
                        setScrollMany(recyclerView, scrollMany);
                        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext(), LinearLayoutManager.VERTICAL, false));
                        break;
                    case DisplayStyle.HORIZONTAL:
                        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
                        break;
                    case DisplayStyle.PAGER_HORIZONTAL:
                        setScrollMany(recyclerView, scrollMany);
                        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
                        break;
                }
                if (itemDecoration == null) {
                    itemDecoration = new GYRecyclerDivider(orientation, dividerPx, dividerColor, dividerPositionList);
                }
                recyclerView.addItemDecoration(itemDecoration);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
            }
            return GYRecyclerViewUtils.this;
        }

        private LinearLayout setScrollMany(RecyclerView recyclerView, boolean scrollMany) {
            if (snapHelper == null){
                if (scrollMany) {
                    snapHelper = new LinearSnapHelper(); //一次可滑动多个
                } else {
                    snapHelper = new PagerSnapHelper();//一次滑动单个
                }
            }
            recyclerView.setOnFlingListener(null);
            snapHelper.attachToRecyclerView(recyclerView);
            return this;
        }
    }


    /**
     * ============================================================================================
     * 网格
     *
     * @param recyclerViews
     * @return
     */
    public GridLayout gridLayout(RecyclerView... recyclerViews) {
        return gridLayout(Arrays.asList(recyclerViews));
    }

    public GridLayout gridLayout(List<RecyclerView> recyclerViewList) {
        return new GridLayout(recyclerViewList);
    }

    public class GridLayout {
        private List<RecyclerView> recyclerViewList;

        public GridLayout(List<RecyclerView> recyclerViewList) {
            this.recyclerViewList = recyclerViewList;
        }

        private int dividerVerticalPx = 0;
        private int dividerVerticalDp = WRAP_CONTENT;
        private int dividerVerticalColor = Color.TRANSPARENT;
        private int dividerHorizontalPx = 0;
        private int dividerHorizontalDp = WRAP_CONTENT;
        private int dividerHorizontalColor = Color.TRANSPARENT;
        private int dividerColorShowUp = DividerColorShowUp.VERTICAL;

        private int spanCount = 4;

        public GridLayout setDividerPx(int dividerPx) {
            setDividerVerticalPx(dividerPx);
            setDividerHorizontalPx(dividerPx);
            return this;
        }

        public GridLayout setDividerDp(int dividerDp) {
            setDividerVerticalDp(dividerDp);
            setDividerHorizontalDp(dividerDp);
            return this;
        }

        public GridLayout setDividerColor(int dividerColor) {
            setDividerVerticalColor(dividerColor);
            setDividerHorizontalColor(dividerColor);
            return this;
        }

        public GridLayout setDividerVerticalPx(int dividerVerticalPx) {
            this.dividerVerticalPx = dividerVerticalPx;
            dividerVerticalDp = WRAP_CONTENT;
            return this;
        }

        public GridLayout setDividerVerticalDp(int dividerVerticalDp) {
            this.dividerVerticalDp = dividerVerticalDp;
            return this;
        }

        public GridLayout setDividerVerticalColor(int dividerVerticalColor) {
            this.dividerVerticalColor = dividerVerticalColor;
            return this;
        }

        public GridLayout setDividerHorizontalPx(int dividerHorizontalPx) {
            this.dividerHorizontalPx = dividerHorizontalPx;
            dividerHorizontalDp = WRAP_CONTENT;
            return this;
        }

        public GridLayout setDividerHorizontalDp(int dividerHorizontalDp) {
            this.dividerHorizontalDp = dividerHorizontalDp;
            return this;
        }

        public GridLayout setDividerHorizontalColor(int dividerHorizontalColor) {
            this.dividerHorizontalColor = dividerHorizontalColor;
            return this;
        }

        public GridLayout setSpanCount(int spanCount) {
            this.spanCount = spanCount;
            return this;
        }

        public GridLayout setDividerColorShowUp(int dividerColorShowUp) {
            this.dividerColorShowUp = dividerColorShowUp;
            return this;
        }

        public GYRecyclerViewUtils build() {
            for (RecyclerView recyclerView : recyclerViewList) {
                if (dividerHorizontalDp != WRAP_CONTENT) {
                    dividerHorizontalPx = GYDisplayUtils.dp2px(recyclerView.getContext(), dividerHorizontalDp);
                }
                if (dividerVerticalDp != WRAP_CONTENT) {
                    dividerVerticalPx = GYDisplayUtils.dp2px(recyclerView.getContext(), dividerVerticalDp);
                }
                recyclerView.setLayoutManager(new GridLayoutManager(recyclerView.getContext(), spanCount));
                if (itemDecoration == null) {
                    itemDecoration = new GYRecyclerDivider(DisplayStyle.GRID, dividerHorizontalPx, dividerHorizontalColor, dividerVerticalPx, dividerVerticalColor, dividerColorShowUp);
                }
                recyclerView.addItemDecoration(itemDecoration);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
            }
            return GYRecyclerViewUtils.this;
        }

    }
}
